<?php

namespace app\common\lib;

class Curl
{

    /**
     * @param $url
     * @param string $method
     * @param array $post_data
     * @return bool|string
     */
    public static function curlGetContents($url,$method='GET',$post_data=[]){
        $ch=curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);                //设置访问的url地址
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);               //设置超时
        if($method=='POST'&&!empty($post_data)){
            //$post_data_string = http_build_query($post_data);
            $post_data_string = json_encode($post_data);
            curl_setopt($ch, CURLOPT_POST, 1);  // 设置使用post方式
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data_string);   //存入转化后的数据字串
        }
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);   //用户访问代理 User-Agent
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($ch, CURLOPT_REFERER,$_SERVER['HTTP_HOST']);        //设置 referer
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);          //跟踪301
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        //返回结果
        $r=curl_exec($ch);
        curl_close($ch);
        return $r;
    }
}